package com.jetgege.smartbox.FirstUse;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;

import com.jetgege.smartbox.Base.App;
import com.jetgege.smartbox.Base.BaseActivity;
import com.jetgege.smartbox.BoxSetting.InputFingerprintActivity;
import com.jetgege.smartbox.R;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class FirstUserActivity extends BaseActivity {

    @BindView(R.id.first_use_toolbar)
    Toolbar firstUseToolbar;
    @BindView(R.id.et_FirstUsePhoneNum)
    EditText etFirstUsePhoneNum;
    @BindView(R.id.ib_FirstUsePhoneClear)
    ImageButton ibFirstUsePhoneClear;
    @BindView(R.id.et_DeviceNumTips)
    EditText etDeviceNumTips;
    @BindView(R.id.ib_DeviceNumClear)
    ImageButton ibDeviceNumClear;
    @BindView(R.id.et_FirstUseConfirmNum)
    EditText etFirstUseConfirmNum;
    @BindView(R.id.ib_FirstUseConfirmNumClear)
    ImageButton ibFirstUseConfirmNumClear;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_first_user;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initValue();
    }

    private void initValue() {

        firstUseToolbar.setTitle("绑定设备");
        setSupportActionBar(firstUseToolbar);

        //调出键盘
        Timer timer = new Timer(true);
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) etFirstUsePhoneNum.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(etFirstUsePhoneNum, 0);
            }
        }, 500);
        imputView();
    }

    private void imputView() {
        etFirstUsePhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ibFirstUsePhoneClear.setVisibility(View.VISIBLE);
                } else ibFirstUsePhoneClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etDeviceNumTips.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ibDeviceNumClear.setVisibility(View.VISIBLE);
                } else ibDeviceNumClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etFirstUseConfirmNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ibFirstUseConfirmNumClear.setVisibility(View.VISIBLE);
                } else ibFirstUseConfirmNumClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.forget_pwd, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        confirm();
        return true;
    }

    /**
     * 验证登录
     */
    private void confirm()
    {
        App.editor.putBoolean("isFirstUse",false);
        App.editor.commit();
        Intent intent =new Intent(FirstUserActivity.this, InputFingerprintActivity.class);
        intent.setAction("FirstUserActivity");
        startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        finish();
    }

    @OnClick({R.id.ib_FirstUsePhoneClear, R.id.ib_DeviceNumClear, R.id.ib_FirstUseConfirmNumClear,R.id.bt_FirstUseConfirmNum})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_FirstUsePhoneClear:
                etFirstUsePhoneNum.setText("");
                break;
            case R.id.ib_DeviceNumClear:
                etDeviceNumTips.setText("");
                break;
            case R.id.ib_FirstUseConfirmNumClear:
                etFirstUseConfirmNum.setText("");
                break;
            case R.id.bt_FirstUseConfirmNum:

                break;
        }
    }

}
